package com.example.minesweepers;

import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
import android.widget.LinearLayout.LayoutParams;

public class MinesweeperView {
	private Activity context;
	private int screenWidth;
	private int screenHeight;
	
	//layout
	private LinearLayout viewsContainer;
	//layout
	
	//first row
	private GameFaceButton smileyFace;
	private Chronometer gameTimer;
	private ChronometerTextView timePassed;
	private FlagsTextView flagsLeft;
	//first row

	//last row
	private ToggleButton flagging;
	//last row
	
	//game rows /w columns
	private ToggleButton[][] minesweeperBlock;
	//game rows /w columns

	public Chronometer getChronometer() {
		return this.gameTimer;
	}
	
	public MinesweeperView(Activity context) {
		this.context = context;
		screenWidth = getScreenSize()[0];
		screenHeight = getScreenSize()[1];
	}
	
	private int[] getScreenSize() {
		Display display = context.getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		int[] widthHeight = {size.x, size.y}; //[0] == width, [1] == heght
		return widthHeight;
	}
	
	/**
	 * The function initializes new views and sets their IDs, where the mines blocks 
	 * ids start from 0 including and end with rowOfMines * colOfMines - 1,
	 * chronometer's id is rowOfMines * colOfMines,
	 * smileyFace's id is rowOfMines * colOfMines + 1,
	 * chronometer text view's id is rowOfMines * colOfMines + 2,
	 * number of flags left text view's id is rowOfMines * colOfMines + 3,
	 * flagging toggle button's id is rowOfMines * colOfMines + 4.
	 * 
	 * @param screenWidth
	 * @param screenHeight 
	 * @param rowOfMines - the number of rows in the mines grid
	 * @param colOfMines - the number of columns in the mines grid
	 */
	public void initializeViews(int rowOfMines, int colOfMines) {
		setupContainer();
		
		setupChronometer(rowOfMines, colOfMines);
		setupChronometerTextView(rowOfMines, colOfMines);
		setupSmileyFace(rowOfMines, colOfMines);
		setupFlagsTextView(rowOfMines, colOfMines);
		setupFlaggingView(rowOfMines, colOfMines);
		setupMinesweeperBlocks(rowOfMines, colOfMines);

		addViewsToLayout(rowOfMines, colOfMines);
	}

	private void addViewsToLayout(int rowOfMines, int colOfMines) {
		LinearLayout viewsContainerRows;
		
		viewsContainerRows = setupContainerRow(screenWidth, screenHeight);
		viewsContainerRows.addView(this.gameTimer);
		viewsContainerRows.addView(this.timePassed);
		viewsContainerRows.addView(this.smileyFace);
		viewsContainerRows.addView(this.flagsLeft);
		this.viewsContainer.addView(viewsContainerRows);
		
		for (int j = 0; j < rowOfMines; j++) {
			viewsContainerRows = setupContainerRow(screenWidth, screenHeight);
			for (int i = 0; i < colOfMines; i++) {
				viewsContainerRows.addView(minesweeperBlock[j][i]);
			}
			this.viewsContainer.addView(viewsContainerRows);
		}
		
		viewsContainerRows = setupContainerRow(screenWidth, screenHeight);
		viewsContainerRows.addView(this.flagging);
		viewsContainerRows.setGravity(Gravity.RIGHT);
		this.viewsContainer.addView(viewsContainerRows);
	}

	private void setupMinesweeperBlocks(int rowOfMines, int colOfMines) {
		minesweeperBlock = new ToggleButton[rowOfMines][colOfMines];
		
		for (int i = 0; i < rowOfMines; i++) {
			for (int j = 0; j < colOfMines; j++) {
				minesweeperBlock[i][j] = new ToggleButton(context);

				ToggleButtonSetup.setupButton(minesweeperBlock[i][j],
						R.drawable.toggle_block,
						(9 * screenWidth / (colOfMines * 10)),
						(8 * screenHeight / (rowOfMines * 10)));
				minesweeperBlock[i][j].setId(j * colOfMines + i);
				minesweeperBlock[i][j].setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if(event.getAction() != (MotionEvent.ACTION_UP)) {
							smileyFace.setBackgroundResource(R.drawable.suprised_face);
						}
						else  {
							smileyFace.setBackgroundResource(R.drawable.smiley_face);
						}
						return false;
					}
				});
			}
		}
	}

	private void setupFlaggingView(int rowOfMines, int colOfMines) {
		flagging = new ToggleButton(context);
		ToggleButtonSetup.setupButton(flagging, R.drawable.flagger, screenWidth/5, screenHeight/15);
		flagging.setId(rowOfMines * colOfMines + 4);
	}

	private void setupFlagsTextView(int rowOfMines, int colOfMines) {
		flagsLeft = new FlagsTextView(context);
		flagsLeft.initializeView(screenWidth, screenHeight);
		flagsLeft.setId(rowOfMines * colOfMines + 3);
	}
	
	private void setupSmileyFace(int rowOfMines, int colOfMines) {
		smileyFace = new GameFaceButton(context);
		smileyFace.setupButton(R.drawable.smiley_face, screenWidth/5, screenHeight/15);
		smileyFace.setGravity(Gravity.BOTTOM);
		smileyFace.setId(rowOfMines * colOfMines + 1);
	}

	private void setupChronometerTextView(int rowOfMines, int colOfMines) {
		timePassed = new ChronometerTextView(context);
		timePassed.initializeView(screenWidth, screenHeight);
		timePassed.setId(rowOfMines * colOfMines + 2);
	}

	private void setupChronometer(int rowOfMines, int colOfMines) {
		gameTimer = new Chronometer(context);
		gameTimer.setLayoutParams(new LayoutParams(0, 0));
		gameTimer.setPadding(0, 0, 0, 0);
		gameTimer.stop();
		gameTimer.setId(rowOfMines * colOfMines);
	}
	
	private LinearLayout setupContainerRow(int screenWidth, int screenHeight) {
		LinearLayout viewsContainerRows = new LinearLayout(context);
		viewsContainerRows.setOrientation(LinearLayout.HORIZONTAL);
		viewsContainerRows.setPadding(0, 0, 0, 0);
		return viewsContainerRows;
	}

	private void setupContainer() {
		viewsContainer = (LinearLayout) context.findViewById(R.id.mineGridLinearLayout);
		viewsContainer.setPadding(0, 0, 0, 0);
		viewsContainer.removeAllViews();
		LinearLayout.LayoutParams gameLayoutParameters = new LayoutParams(LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT);
		gameLayoutParameters.setMargins(screenWidth / 20, 0, screenWidth / 20, 0);
		viewsContainer.setLayoutParams(gameLayoutParameters);
	}
	
	/**
	 * The function returns the current time passed since the first press of a button
	 * @return the seconds as a string ( 0 - 999 )
	 */
	public String getTime() {
		return timePassed.getText().toString();
	}
}